package com.overlord.im.server.resolver.message;

import com.google.common.collect.Lists;
import com.overlord.im.commons.message.OverlordImMessage;
import com.overlord.im.server.resolver.MessageResolver;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName MessageResolverFactory
 * @Author wuwei
 * @Description NONE
 * @Date 2020/6/2 17:56
 **/
@Service
public class MessageResolverFactory implements InitializingBean {

    private static final List<MessageResolver> RESOLVERS = Lists.newArrayList();

    @Resource
    private MsgrResolver msgrResolver;
    @Resource
    private AckrResolver ackrResolver;

    public static void registerResolver(MessageResolver resolver) {
        RESOLVERS.add(resolver);
    }

    public static MessageResolver resolver(OverlordImMessage message) {
        for (MessageResolver resolver : RESOLVERS) {
            if (resolver.support(message)) {
                return resolver;
            }
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        MessageResolverFactory.registerResolver(msgrResolver);
        MessageResolverFactory.registerResolver(ackrResolver);
    }
}
